package pa;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Formatter;
import java.util.StringTokenizer;

import com.muzo.common.crypto.Digest;
import com.sun.org.apache.bcel.internal.classfile.Signature;
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import com.sun.org.apache.xml.internal.security.utils.DigesterOutputStream;

import sun.security.provider.DSAPrivateKey;

public class Main {

	/**
	 * @param args
	 * @throws NoSuchProviderException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws SignatureException 
	 */
	 private static String byteArray2Hex(byte[] hash) {
	        Formatter formatter = new Formatter();
	        for (byte b : hash) {
	            formatter.format("%02x", b);
	        }
	        return formatter.toString();
	    }
	public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, 
	SignatureException, Base64DecodingException,
	KeyStoreException, CertificateException, 
	IOException, UnrecoverableKeyException, KeyException {
		
		KeyStore ks = KeyStore.getInstance("JKS");
		char[] pass = "changeit".toCharArray();
		FileInputStream fis = new FileInputStream(new File("/home/dima/test.ks"));
		ks.load(fis, pass);
		fis.close();
		PrivateKey pk = (PrivateKey) ks.getKey("paymuzo", pass);
		System.out.println(pk.getFormat());
		String mes = "2734601054|CREATE_ORDER|126019299255|100|203|1|124759102034|https://www.testm"+
				"erchant.com/response.jsp|Nakup|B8E5AD3CEBE760E95921FCBC4D92C7";
		
		//String str = Digest.sign(mes,pk);
		Formatter f = new Formatter();
		MessageDigest sha1 = MessageDigest.getInstance("SHA1");
		sha1.update(mes.getBytes());
		byte[] d = sha1.digest();
		String hash = byteArray2Hex(d);
		
		String res = "";
		//System.out.println(str);
		StringTokenizer stk = new StringTokenizer(mes,"|");
		//String sl = (String) stk.nextElement();
		//System.out.print(byteArray2Hex(sl.getBytes()));
		while(stk.hasMoreElements()){
			res+=byteArray2Hex(((String)stk.nextElement()).getBytes())+"|";
		}
		res+=hash;
		String result = Digest.sign("2734601054|CREATE_ORDER|126019299255|100|203|1|124759102034|https://www.testm"+
				"erchant.com/response.jsp|Nakup|B8E5AD3CEBE760E95921FCBC4D92C7", pk);
		System.out.println(URLEncoder.encode(result,"UTF-8"));
		
//		byte[] bt = "2734601054|CREATE_ORDER|126019299255|100|203|1|124759102034|https://www.testmerchant.com/response.jsp|Nakup|B8E5AD3CEBE760E95921FCBC4D92C7".getBytes();
//		System.out.println(byteArray2Hex(bt));
//		KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
//		SecureRandom sec = SecureRandom.getInstance("SHA1PRNG","SUN");
//		kpg.initialize(2048, sec);
//		KeyPair kp = kpg.generateKeyPair();
//		PrivateKey pk = kp.getPrivate();
		
//		java.security.Signature sig = java.security.Signature.getInstance("SHA1withRSA");
//		sig.initSign(pk);
//		String mes = "2734601054|CREATE_ORDER|126019299255|100|203|1|124759102034|https://www.testm"+
//"erchant.com/response.jsp|Nakup|B8E5AD3CEBE760E95921FCBC4D92C7";
//		byte[] bmes =  mes.getBytes();
//		sig.update(bmes);
//		byte[] signa = sig.sign();
////		
//		String sss = Base64.encode(signa);
//		System.out.println(sss.length());
//		System.out.print(sss);
		
//		sig.initVerify(kp.getPublic());
//		sig.update(mes.getBytes());
//		System.out.println(sig.verify(signa));
		//////////////////////////////////
		
		/////////////////////////////////

	}

}
